package com.zm.demo.mysql.connect;

import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class TypeConvertDemo {

    public static void main(String[] args) throws Exception {
        Long start = System.currentTimeMillis();
        connectionProperties();
        System.out.println("耗时： " + (System.currentTimeMillis() - start) + " ms");
    }

    private static void connectionProperties() throws Exception{
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            String url = "jdbc:mysql://localhost:3306/local1?user=root&password=root";
            connection = DriverManager.getConnection(url);
            statement = connection.createStatement();
//            statement.setFetchSize(Integer.MIN_VALUE);
            String sql = "select * from user";
            rs = statement.executeQuery(sql);
            int count=0;
            System.out.println(String.format("内存使用量： %s MB", (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory())/1024/1024));
            while (rs.next()){
                Reader reader = rs.getCharacterStream("name");
                char[] buf = new char[1000];
                reader.read(buf);
                System.out.println(new String(buf));
                count++;
                break;
            }
            System.out.println("connection by properties: " + count);
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            close(connection,statement,rs);
        }
    }

    private static void close(Connection connection, Statement statement, ResultSet rs) throws Exception{
        if (rs != null){
            rs.close();
        }

        if (statement != null){
            statement.close();
        }

        if (connection != null){
            connection.close();
        }
    }
}
